<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto">
            <el-card shadow="hover" style="display: flex; flex-direction: column; flex: 1; height: inherit">
				<section style="flex: 1; height: inherit;overflow-y: auto;">
                    <el-form ref="ruleFormRef" :model="state.agentData" :rules="rules" label-width="260px">
                        <section class="font16 px10 pl20 borderRadius5" style="background:var(--el-color-primary-light-9);">
                            <p>资料设置</p>
                        </section>
                        <el-form-item label="代理资料填写审核：">
                            <el-radio-group v-model="state.agentData.agentJoinTask">
                                <el-radio :label=0>自动审核</el-radio>
                                <el-radio :label=1>手动审核</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item class="mt10" label="代理资料选中：" prop="agentLevel">
                            <el-checkbox-group>
                                <el-checkbox label="身份证" value="Value A" />
                                <el-checkbox label="地区" value="Value B" />
                                <el-checkbox label="学历" value="Value C" />
                                <el-checkbox label="出生年月" value="Value disabled"  />
                                <el-checkbox label="性别" value="Value disabled"  />
                                <el-checkbox label="微信号" value="Value disabled"  />
                            </el-checkbox-group>
                        </el-form-item>
                        <section class="font16 px10 pl20 borderRadius5" style="background:var(--el-color-primary-light-9);">
                            <p>资料高级设置</p>
                        </section>
                        <el-form-item label="当代理升级时存在新的资料：">
                            <el-radio-group v-model="state.agentData.agentJoinTask">
                                <el-radio :label=0>申请升级时完善</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="代理新资料审核设置：">
                            <el-radio-group v-model="state.agentData.agentJoinTask">
                                <el-radio :label=0>自动审核</el-radio>
                                <el-radio :label=1>手动审核</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-form>
                </section>
                <section style="text-align: center; margin:10px 0;">
                    <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
                </section>
            </el-card>
            <AddInformation ref="addInformationRef" @selectFun="selectFun" />
            <useTypeDialog ref="useTypeDialogRef" @useTypeListFun="useTypeListFun" />
            <goodSelectDialog ref="goodSelectDialogRef" @goodsSubmitFun="goodsSubmitFun" />
        </div>
	</div>
</template>
<script setup lang="ts" name="funWangEditor">
import { reactive, onMounted, ref, defineAsyncComponent } from 'vue';
import { ElMessage } from 'element-plus';
import { couponAdd,couponInfo } from "/@/api/marketing";
import { configData } from '/@/api/common';
import { brandList, productCategoryList } from "/@/api/commodity";
import type { FormInstance, FormRules } from 'element-plus'
import { useRoute } from 'vue-router';
const route = useRoute();
const AddInformation = defineAsyncComponent(() => import('/@/components/information/AddInformation.vue'))
const useTypeDialog = defineAsyncComponent(() => import('/@/components/createGoods/useTypeDialog.vue'))
// 引入组件
const goodSelectDialog = defineAsyncComponent(() => import('/@/components/dialog/goodSelect.vue'));
// 定义变量内容
const goodSelectDialogRef = ref();
const useTypeDialogRef = ref()
const emit = defineEmits(['stepChange'])
const ruleFormRef = ref<FormInstance>()
const addInformationRef = ref()
const state = reactive({
	// 添加图片类型
	addFileType: '',
	// 商品类型列表
	brandList: [] as Array<any>,
	// 门店商品分类
	storeCategoryList: [] as Array<any>,
	// 商城商品分类
	shopCategoryList: [] as Array<any>,
	// 运费模板列表
	productBrandList: [] as Array<any>,
    productTypeList: [] as Array<any>,
    agentData:{
        agentLevel: 0, //代理权重等级
        agnetName: '', // 代理级别名称
        agentPurchaseDiscount: '', // 代理进货折扣
        agentIsRecharge: 0, // 代理充值权限
        agentIsLoans: 0,//总分充值货款账户权限：
        agentMinRecharge: null,//最低充值额度
        agentJoinTask: 0,//加入任务：0无任务 1 有任务
        agentTaskType: 0,//任务类型 首进任务或者折扣任务
        agentTaskPurchase: 0,//首进对象：0总部 1上级
        agentTaskPurchaseType: 0,//首进类型：0代理选购 1组合套餐
        agentTaskPurchaseMoney: null,//任务金额
        agentTaskPurchaseDiscountType: 0, // 收进折扣类型 0 默认折扣 1 自定义折扣
        agentTaskPurchaseDiscountValue: 0,// 首进折扣 自定义折扣数值
        agentMinPurchase: null,//最低进货额度
        agentValidMonth: null,//授权有效期
        agentPrompt: '',//注册提示语句
    },
    voucherData:{
        skuId:null,
        couponBaseType:0,
        couponName:'',//优惠券名称
        couponMinPriceType:0,//是否存在使用门槛
        couponMinPrice:null, // 最低消费金额
        couponValue:null, // 金额或者折扣值
        couponType: 1,// 领取方式 1手动领取 2新人 3 赠送
        couponUseType: 1, // 使用类型
        goodsList:[],
        couponProductIsUse:1 , // 0 不可用 1 可用
        normalIds:[],
        shopCategoryIds:[],
        storeCategoryIds:[],
        couponIsForever: 0, // 领取时间限制
        couponIsForeverTime: [], // 领取时间段
        couponIsFixedTime: 0, // 是否固定使用时间
        couponUseDay:null, // 固定可使用天数
        couponIsFixedTimeDate: [], // 使用时间段
        couponLimitType:0, // 优惠使用限制
        couponIsLimited: 0, // 优惠券是否限量发放
        couponTotal:null, // 优惠券发放数量
        couponDesc: '', // 使用规则
        couponStatus: 0, // 状态 0 关闭 1 开启
    },
    tableData:[{}]
})

const rules = reactive<FormRules<any>>({
	couponBaseType: [{ required: true, message: '请选择优惠券类型', trigger: 'change' }],
	name: [{ required: true, message: '请输入优惠券名称', trigger: 'blur' }],
	money: [{ required: true, message: '请选择优惠券面值', trigger: 'blur' }],
	type: [{ required: true, message: '请选择优惠券领取方式', trigger: 'change' }],
	useType: [{ required: true, message: '请选择优惠券使用类型', trigger: 'change' }],
})

// 商品类型修改 不同类型切换时会清空Sku数据或者设置sku 为单规格
const changeType = (val: any) => {
	// if(goodsInfoData.status == 'edit'){
	// 	return ElMessage({
	// 		type: 'warning',
	// 		message: '商品编辑时商品类型不可修改',
	// 	})
	// }
    // 修改金额或者折扣值
	if(val != state.voucherData.couponBaseType){
        state.voucherData.couponBaseType = val;
        state.voucherData.couponValue = null;
	}
}

// 使用类型选择弹窗
const addGoodsFile = () => {
    goodSelectDialogRef.value.openDialog(state.voucherData.goodsList);
}

// 选中关联商品
const goodsSubmitFun = (list: any) =>{
	state.voucherData.goodsList = list.map((item:any) => {return {data:item}});
}

// 页面加载时
onMounted(async ()  => {
    getChainList();
    getBrandList();
    getStoreCategoryList();
    getShopCategoryList();
    if (route.query.status == 'edit') {
        const { results } = await couponInfo(route.query.id);
        state.voucherData.skuId = results.coupon.skuId;
        state.voucherData.couponBaseType = results.coupon.couponBaseType;
        state.voucherData.couponName = results.coupon.couponName;//优惠券名称
        state.voucherData.couponMinPriceType = results.coupon.couponMinPrice > 0 ? 1 : 0;//是否存在使用门槛
        state.voucherData.couponMinPrice = results.coupon.couponMinPrice; // 最低消费金额
        state.voucherData.couponValue = results.coupon.couponValue; // 金额或者折扣值
        state.voucherData.couponType = results.coupon.couponType;// 领取方式 1手动领取 2新人 3 赠送
        state.voucherData.couponUseType = results.coupon.couponUseType; // 使用类型
        state.voucherData.goodsList = results.product; 
        state.voucherData.couponProductIsUse = results.coupon.couponProductIsUse;  // 0 不可用 1 可用
        state.voucherData.normalIds = results.coupon.normalIds;
        state.voucherData.shopCategoryIds = results.coupon.shopCategoryIds;
        state.voucherData.storeCategoryIds = results.coupon.storeCategoryIds;
        state.voucherData.couponIsForever = results.coupon.couponIsForever; // 领取时间限制
        state.voucherData.couponIsForeverTime = results.coupon.couponIsForeverTime; // 领取时间段
        state.voucherData.couponIsFixedTime = results.coupon.couponIsFixedTime;  // 是否固定使用时间
        state.voucherData.couponUseDay = results.coupon.couponUseDay;  // 固定可使用天数
        state.voucherData.couponIsFixedTimeDate = results.coupon.couponIsFixedTimeDate; // 使用时间段
        state.voucherData.couponLimitType = results.coupon.couponLimitType; // 优惠使用限制
        state.voucherData.couponIsLimited = results.coupon.couponIsLimited; // 优惠券是否限量发放
        state.voucherData.couponTotal = results.coupon.couponTotal; // 优惠券发放数量
        state.voucherData.couponDesc = results.coupon.couponDesc; // 使用规则
        state.voucherData.couponStatus = results.coupon.couponStatus; // 状态 0 关闭 1 开启
    }
});

// 获取商品类型列表
const getChainList = async () => {
	const { results } = await configData({ dictType: '' });
	state.productTypeList = results.spuType;
	state.productTypeList.forEach((Obj: any) => {
		if (Obj.children != null && Obj.children.length > 0) {
			Obj.disabled = false
		}
	})
}

// 品牌列表获取
const getBrandList = async () => {
	const { results } = await brandList({ mode: null });
	state.productBrandList = results.records;
    state.productTypeList.forEach((Obj: any) => {
		if (Obj.children != null && Obj.children.length > 0) {
			Obj.disabled = false
		}
	})
}

// 门店商品分类获取
const getStoreCategoryList = async () => {
	const { results } = await productCategoryList({ platformType: 0 });
	state.storeCategoryList = results;
    state.storeCategoryList.forEach((Obj: any) => {
		if (Obj.children != null && Obj.children.length > 0) {
			Obj.disabled = false
		}
	})
}

// 商城商品分类获取
const getShopCategoryList = async () => {
	const { results } = await productCategoryList({ platformType: 1 });
	state.shopCategoryList = results;
    state.shopCategoryList.forEach((Obj: any) => {
		if (Obj.children != null && Obj.children.length > 0) {
			Obj.disabled = false
		}
	})
}

const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return
	await formEl.validate( async (valid, fields) => {
		if (valid) {
			let params = {
                skuId: state.voucherData.skuId,
                couponBaseType: state.voucherData.couponBaseType,
                couponName:state.voucherData.couponName,//优惠券名称
                couponMinPrice: state.voucherData.couponMinPriceType == 0 ? 0 : state.voucherData.couponMinPrice, // 最低消费金额
                couponValue:Number(state.voucherData.couponValue), // 金额或者折扣值
                couponType: state.voucherData.couponType,// 领取方式 1手动领取 2新人 3 赠送
                couponUseType: state.voucherData.couponUseType, // 使用类型
                normalIds: state.voucherData.couponUseType == 2 ? state.voucherData.goodsList.map((obj:any) => obj.data.skuId) : state.voucherData.normalIds,
                shopCategoryIds:state.voucherData.shopCategoryIds,
                storeCategoryIds:state.voucherData.storeCategoryIds,
                couponProductIsUse: state.voucherData.couponProductIsUse,
                couponIsForever: state.voucherData.couponIsForever, // 领取时间限制
                couponReceiveStartTime: state.voucherData.couponIsForever == 1 ? state.voucherData.couponIsForeverTime[0] : '',
                couponReceiveEndTime: state.voucherData.couponIsForever == 1 ? state.voucherData.couponIsForeverTime[1] : '',
                couponIsFixedTime: state.voucherData.couponIsFixedTime, // 是否固定使用时间
                couponUseDay:Number(state.voucherData.couponUseDay), // 固定可使用天数
                couponUseStartTime: state.voucherData.couponIsFixedTime == 1 ? state.voucherData.couponIsFixedTimeDate[0] : '',
                couponUseEndTime: state.voucherData.couponIsFixedTime == 1 ? state.voucherData.couponIsFixedTimeDate[1] : '',
                couponLimitType: state.voucherData.couponLimitType, // 优惠使用限制
                couponIsLimited: state.voucherData.couponIsLimited, // 优惠券是否限量发放
                couponTotal: state.voucherData.couponIsLimited == 0 ? 0 : state.voucherData.couponTotal, // 优惠券发放数量
                couponDesc: state.voucherData.couponDesc, // 使用规则
                couponStatus: Number(state.voucherData.couponStatus), // 状态 0 关闭 1 开启
            }
            const { results } = await couponAdd(params);
            console.log(results)
		} else {
			console.log('error submit!', fields)
		}
	})
}

</script>

<style lang="scss" scoped>
:deep(.el-card__body) {
	display: flex;
	flex-direction: column;
	flex: 1;
	height: inherit;
}
.el-form{
    flex:1;
    overflow-y: auto;
    position: relative;
}
.form-ele-size {
    width: 260px;
}

.line-title {
    font-size: 18px;
    padding: 8px 12px;
    background-color: #eceef0;
    border-radius: 6px;
    margin-bottom: 15px;
}

.selector-img {
    // width: 64px;
    // height: 64px;
    // border: 1px solid #ededed;
    // border-radius: 5px;
    // position: relative;
    // display: flex;
    // justify-content: center;
    // align-items: center;
    // &:hover {
    //     background-color: #f5f5f5;
    //     border: 1px solid #e4e3e3;
    // }
}
.typeBox{
    height: 65px;
    margin-right: 15px;
    border: 1px solid var(--el-border-color-light, #ebeef5);
    line-height: 24px;
    box-sizing: border-box;
    cursor: pointer;
    span:first-child{
        font-weight: 600;
    }
    span:last-child{
        font-weight: 500;
        color: var(--el-color-info-light-3);
    }
}
.typeBoxSelected {
    border: 1px solid var(--el-color-primary);
    position: relative;
    .virtual_san{
        position: absolute;
        bottom: 0;
        right: 0;
        width: 0;
        height: 0;
        border-bottom: 26px solid var(--el-color-primary);
        border-left: 26px solid transparent;
    }
    .virtual_dui{
        position: absolute;
        bottom: -2px;
        right: 2px;
        color: #fff;
        font-family: system-ui;
    }
}
</style>
